package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.adapter.BindThirdAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.ACCOUNTBean;
import com.kpx.app.bean.BindCardInfo;
import com.kpx.app.bean.ThirdInfoBean;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/7.
 * 绑定第三方账号
 */

public class ActivityBindThird extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    private List<ThirdInfoBean> tlist = new ArrayList<>();
    private BindThirdAdapter adapter;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_bind_third);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "绑定第三方账号");
        UIUtils.initRefreshView(outView);
        outView.setAutoLoadMore(false);
        adapter = new BindThirdAdapter(this, tlist, R.layout.item_bind_third);
        lvShow.setAdapter(adapter);
        lvShow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                mShareAPI.getPlatformInfo(UserinfoActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                if(tlist.get(position)==null){
                    ToastUtil.getInstance().showToast(ActivityBindThird.this, "信息有误，正在重新获取数据！");
                    getData();
                    return;
                }
                ActivityBindThirdTo.thirdNum = tlist.get(position);
                IntentUtil.gotoActivity(ActivityBindThird.this, ActivityBindThirdTo.class);
            }
        });
        getData();
    }

    @Override
    public void initData() {
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                getData();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                outView.stopLoadMore();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<ACCOUNTBean> list = UserInfoUtils.getCachedLoginUser().getACCOUNT();
        if(list!=null){
            for (int i = 0; i < tlist.size(); i++) {
                for (int j = 0; j < list.size(); j++) {
                    if (tlist.get(i).getDmId() == list.get(j).getPlatformId()) {
                        tlist.get(i).setBind(true);
                    }
                }
            }
        }
        adapter.setData(tlist);
    }

    /**
     * 获取第三方绑定数据
     */
    private void getData() {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                outView.stopRefresh();
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        tlist.clear();
                        tlist.addAll((Collection<? extends ThirdInfoBean>) result.getData());
                        List<ACCOUNTBean> list = UserInfoUtils.getCachedLoginUser().getACCOUNT();
                        if(list!=null){
                            for (int i = 0; i < tlist.size(); i++) {
                                for (int j = 0; j < list.size(); j++) {
                                    if (tlist.get(i).getDmId() == list.get(j).getPlatformId()) {
                                        tlist.get(i).setBind(true);
                                    }
                                }
                            }
                        }
                        adapter.setData(tlist);
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityBindThird.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                outView.stopRefresh();
                ToastUtil.getInstance().showToast(ActivityBindThird.this, error);
            }

        }, ThirdInfoBean.class, HttpDataRequest.TYPE_LIST).POST(HttpDataReqUrl.URL_POST_GET_THIRD_LIST, true, map);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

}
